package cn.com.victorysoft.ylgl.service.fracturingpressure.controller;

import cn.com.victorysoft.ylgl.service.commons.web.JsonMessage;
import cn.com.victorysoft.ylgl.service.commons.web.RequestContext;
import cn.com.victorysoft.ylgl.service.commons.web.User;
import cn.com.victorysoft.ylgl.service.fracturingpressure.domain.ConstructionTechnologyChild;
import cn.com.victorysoft.ylgl.service.fracturingpressure.service.fracturing.IConstructionTechnologyChildService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 施工工艺子表
 * Created by Administrator on 18/3/16.
 */
@Controller
@RequestMapping({"fracturingPressure/constructionTechnologyChild"})
public class ConstructionTechnologyChildController {
    private static final Logger logger = LoggerFactory.getLogger(ConstructionTechnologyChildController.class);

    @Resource
    private IConstructionTechnologyChildService service;

    public ConstructionTechnologyChildController() {
    }

    @RequestMapping(
            value = {"/list.wet"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String list() {
        return "fracturingPressure/pressureConstruction/detail";
    }

    @RequestMapping(
            value = {"/getEntity.wds"},
            method = {RequestMethod.POST,RequestMethod.GET}
    )
    @ResponseBody
    public JsonMessage getEntity(@RequestParam(value="id", required=true) String id) {
        ConstructionTechnologyChild entity = this.service.getById(id);
        return (new JsonMessage()).success(entity);
    }

    @RequestMapping(
            value = {"/add.wds"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public JsonMessage createEntity(@ModelAttribute("model") ConstructionTechnologyChild entity) {
        User user = RequestContext.getCurrentUser();
        entity.setCreateUserId(user.getName());
        entity.setCreateDate(new Date());
        this.service.insert(entity);
        ConstructionTechnologyChild newEntity = service.getById(entity.getDataId());
        return (new JsonMessage()).success(newEntity);
    }


    @RequestMapping(
            value = {"/update.wds"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public JsonMessage updateEntity(@ModelAttribute("model") ConstructionTechnologyChild entity) {
        ConstructionTechnologyChild oldEntity = service.getById(entity.getDataId());

        User user = RequestContext.getCurrentUser();
        entity.setUpdateUserId(user.getName());
        entity.setUpdateDate(new Date());

        this.service.updateByPrimaryKey(entity);
        ConstructionTechnologyChild newEntity = service.getById(entity.getDataId());
        return (new JsonMessage()).success(newEntity);
    }


    /**
     * @param ids:1;2;3;
     * @return
     */
    @RequestMapping(value={"/delete.wds"}, method={RequestMethod.POST}, produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonMessage delete(@RequestParam(value="ids", required=true) String ids)
    {
        try
        {
            this.service.deleteByKeys(ids);
            return new JsonMessage().success();
        }
        catch (Exception e)
        {
            logger.error("删除出错: {}", e);
        }
        return new JsonMessage().failure("失败");
    }

    @RequestMapping(
            value = {"/queryList.wds"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public JsonMessage queryList(@RequestParam(value = "parentId",required = true) String parentId) {

        List<ConstructionTechnologyChild> dataList =  this.service.queryList(parentId);
        return (new JsonMessage()).success(dataList);
    }
}